/*
 *  SurfacePlane.cpp
 *  EPPI-0.0
 *
 *  Created by Roberto Ortega on 07/06/11
 *
 */

#include "surfaceplane.h"

// ***
SurfacePlane::SurfacePlane()
:Surface()
{}

// ***
SurfacePlane::~SurfacePlane()
{}

// ***
SurfacePlane::SurfacePlane(ifstream &in, const string& na)
:Surface(na)
{
	dx = 1.0;
	dy = 1.0;
	
	nx = 0.0;
	ny = 0.0;
	nz = 1.0;
	
	nn = 10;
	mm = 10;
	
	string keyword;

	while (in >> keyword && keyword.compare("ENDSURFACE") )
	{		
		if (!keyword.compare("DIMS"))
		{
			in >> dx;
			in >> dy;
			
			cout << keyword << endl;
			cout << dx <<"\t" << dy <<"\t" << endl;
		}
		if (!keyword.compare("GRID"))
		{
			in >> nn;
			in >> mm;
			
			cout << keyword << "\t";
			cout << nn << "\t" << mm << endl;
		}
		if (!keyword.compare("CONTACTPOINTS"))
		{
			in >> pointsOption;
			
			if (!pointsOption.compare("all"))
			{
				contactAllOption = true;
				
				cout << keyword << "\t";
				cout << pointsOption << endl;
			}
			
			if (!pointsOption.compare("file"))
			{
				in >> contactFileName;
				contactFileOption = true;
				
				cout << keyword << "\t";
				cout << pointsOption << "\t" << contactFileName << endl; 
			}
		}		
	}
	
	createPlane();
	this->createContactPoints();
}

// ***
void SurfacePlane::createPlane(void)
{
	int i;
	int j;
	
	double x;
	double y;
	double z;
	
	double u;
    double v;
	
	int elem;
    int node;
	
	vector<double> vCoordinates;
    vector<int> vElement;
	
	vCoordinates.resize(3);
	
	for (j = 0; j < mm + 1; ++j)
	{
		for (i = 0; i < nn + 1; ++i)
		{
			u = dx / nn * i;
			v = dy / mm * j;
			
			node = i + j * (nn + 1);
			
			x = -dx / 2 + u;
			y = -dy / 2 + v;
			z = 0.0;
			
			vCoordinates[0] = x;
			vCoordinates[1] = y;
			vCoordinates[2] = z;
			
			vLocalPoints.push_back(vCoordinates);
		}
	}
	
	vElement.resize(4);
	
	for (j = 0; j < mm; ++j)
	{
		for (i = 0; i < nn; ++i)
		{
			elem = i + j * nn;
			
			vElement[0] = elem + j;
			vElement[1] = elem + j + 1;
			vElement[2] = elem + j + nn + 2;
			vElement[3] = elem + j + nn + 1;
			
			vLocalElements.push_back(vElement);
		}
	}
	
	nLocalPoints = vLocalPoints.size();
	nLocalElements = vLocalElements.size();
}

// ***
double SurfacePlane::evalS(double& x, double& y, double& z)
{
	S = (x * nx + y * ny + z * nz);
	
	return S;
}

//***
Eigen::RowVector3d SurfacePlane::evaldS(double& x, double& y, double& z)
{
	dS.x() = nx;
	dS.y() = ny;
	dS.z() = nz;
	
	return dS;
}

//***
Eigen::Matrix3d SurfacePlane::evalddS(double& x, double& y, double& z)
{
	return ddS;
}